Telegram Group & Telegram Channel
56. Matrix Operations.

import java.util.Scanner;

public class Matrices {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();

System.out.print("Enter number of columns: ");
int columns = scanner.nextInt();

System.out.println();
System.out.println("Enter first matrix");
int[][] a = readMatrix(rows, columns);

System.out.println();
System.out.println("Enter second matrix");
int[][] b = readMatrix(rows, columns);

int[][] sum = add(a, b);
int[][] difference1 = subtract(a, b);
int[][] difference2 = subtract(b, a);

System.out.println();
System.out.println("A + B =");
printMatrix(sum);

System.out.println();
System.out.println("A - B =");
printMatrix(difference1);

System.out.println();
System.out.println("B - A =");
printMatrix(difference2);
}

public static int[][] readMatrix(int rows, int columns) {

int[][] result = new int[rows][columns];
Scanner s = new Scanner(System.in);

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = s.nextInt();

}
}
return result;
}

public static int[][] add(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] + b[i][j];

}
}
return result;
}

public static int[][] subtract(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] - b[i][j];

}
}
return result;
}

public static void printMatrix(int[][] matrix) {

int rows = matrix.length;
int columns = matrix[0].length;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(matrix[i][j] + " ");

}
System.out.println();
}
}
}

@java_codings



tg-me.com/java_codings/76
Create:
Last Update:

56. Matrix Operations.

import java.util.Scanner;

public class Matrices {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();

System.out.print("Enter number of columns: ");
int columns = scanner.nextInt();

System.out.println();
System.out.println("Enter first matrix");
int[][] a = readMatrix(rows, columns);

System.out.println();
System.out.println("Enter second matrix");
int[][] b = readMatrix(rows, columns);

int[][] sum = add(a, b);
int[][] difference1 = subtract(a, b);
int[][] difference2 = subtract(b, a);

System.out.println();
System.out.println("A + B =");
printMatrix(sum);

System.out.println();
System.out.println("A - B =");
printMatrix(difference1);

System.out.println();
System.out.println("B - A =");
printMatrix(difference2);
}

public static int[][] readMatrix(int rows, int columns) {

int[][] result = new int[rows][columns];
Scanner s = new Scanner(System.in);

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = s.nextInt();

}
}
return result;
}

public static int[][] add(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] + b[i][j];

}
}
return result;
}

public static int[][] subtract(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] - b[i][j];

}
}
return result;
}

public static void printMatrix(int[][] matrix) {

int rows = matrix.length;
int columns = matrix[0].length;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(matrix[i][j] + " ");

}
System.out.println();
}
}
}

@java_codings

BY Advance Java πŸ‘¨β€πŸ’»


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/java_codings/76

View MORE
Open in Telegram


Advance Java ‍ Telegram | DID YOU KNOW?

Date: |

Start with a fresh view of investing strategy. The combination of risks and fads this quarter looks to be topping. That means the future is ready to move in.Likely, there will not be a wholesale shift. Company actions will aim to benefit from economic growth, inflationary pressures and a return of market-determined interest rates. In turn, all of that should drive the stock market and investment returns higher.

Importantly, that investor viewpoint is not new. It cycles in when conditions are right (and vice versa). It also brings the ineffective warnings of an overpriced market with it.Looking toward a good 2022 stock market, there is no apparent reason to expect these issues to change.

Advance Java ‍ from ru


Telegram Advance Java πŸ‘¨β€πŸ’»
FROM USA